package com.caifu.bean;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.awt.*;
import java.awt.print.*;
import java.util.Map;

/**
 * @Auther: Lyf
 * @Date: 2020/12/22 16:59
 * @Description:
 */
public class PrintUtil implements Printable {

    private static Map<String, Object> data = null;

    @Override
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

        if (page > 0) {
            return NO_SUCH_PAGE;
        }

        Graphics2D g2d = (Graphics2D) g;
        JSONObject info = JSONObject.fromObject(data.get("info"));
        JSONObject fixedPoint = JSONObject.fromObject(data.get("fixedPoint"));
        JSONArray toDoor = JSONArray.fromObject(data.get("toDoor"));

        g2d.setFont(new Font("Default", Font.PLAIN, 12));
        g2d.drawString(getNotStr(info.get("title")), 50, 10);
        g2d.drawString(getNotStr(info.get("date")), 65, 30);
        g2d.drawString("--------------------堂吃/自提-------------------", 0, 50);
        g2d.drawString("堂吃/自提      x"+getNotStr(fixedPoint.get("count")), 10, 70);
        g2d.drawString(getNotStr(fixedPoint.get("address")), 10, 90);
        if(toDoor != null && toDoor.size() > 0) {
            g2d.drawString("--------------------送餐上门--------------------", 0, 110);
            g2d.drawString("送餐上门       x" + toDoor.size(), 10, 130);
            int y = 150;
            for(int i = 0 ; i < toDoor.size() ; i ++) {
                JSONObject obj = toDoor.getJSONObject(i);
                g2d.drawString(getNotStr(obj.get("user_name")) + "          "+ getNotStr(obj.get("mobile")), 10, y);
                g2d.drawString(getNotStr(obj.get("address")), 10, y+20);
                y = y + 40;
            }
        }
        return PAGE_EXISTS;
    }

    public static void init(java.util.List<Map<String, Object>> dataList) {

        if(dataList != null && dataList.size() > 0) {
            for(Map<String,Object> map : dataList){
                data = map;

                int height = 175 + 3 * 15 + 20;

                // 通俗理解就是书、文档
                Book book = new Book();

                // 打印格式
                PageFormat pf = new PageFormat();
                pf.setOrientation(PageFormat.PORTRAIT);

                // 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
                Paper p = new Paper();
                p.setSize(230, height);
                p.setImageableArea(5, -20, 230, height + 20);
                pf.setPaper(p);

                // 把 PageFormat 和 Printable 添加到书中，组成一个页面
                book.append(new PrintUtil(), pf);

                // 获取打印服务对象
                PrinterJob job = PrinterJob.getPrinterJob();
                job.setPageable(book);
                try {
                    job.print();
                } catch (PrinterException e) {
                    e.printStackTrace();
                    System.out.println("================da yin chu xian yi chang");
                }
            }
        }
    }


    public String getNotStr(Object obj){
        if(obj != null && StringUtils.isNotBlank(String.valueOf(obj)) && !"null".equals(String.valueOf(obj))){
            return String.valueOf(obj);
        }else{
            return "";
        }
    }

}
